<?php

namespace app\index\controller;

use think\Controller;
use think\Validate;


/**
 * @title app更新（模块18）
 * @description 接口说明
 * @group 检测app版本
 * @header name:model_id   default:05 desc:模块id
 * @groupremark 已使用错误码：1130001 1130002 1130003 1130004 1130005 1130006 1130007 1130008 1130009 1130010 1130011 1130012
 */
class Apk extends Controller
{
    /**
     * @title 检测app版本
     * @description 接口说明
     * @author 邓汉炜
     * @url /index/apk/index
     * @param name:api_ver type:string require:1  other: desc:默认1.0
     * @param name:app_ver type:string require:1  other: desc:当前版本号
     * @param name:app_build_ver type:string require:1  other: desc:最新的build版本号
     * @param name:app_type type:string require:1  other: desc:设备类型
     *
     * @method ~
     */
    public function index($param = [])
    {
        $data = input();
        logs('apk',$data);
        if (empty($data) && !empty($param)) {
            $data = $param;
        }
        //验证数据
        $rule = [
           'api_ver'=>'require',
           'app_ver'=>'require',
//            'app_build_ver' => 'require',
            'app_type' => 'require',
        ];
        // 验证信息
        $message = [
           'api_ver.require' => '默认api版本不能为空',
           'app_ver.require' => '当前版本不能为空',
//           'app_build_ver.require' => '最新版本号不能为空',
           'app_type.require' => '设备类型不能为空',
        ];
        $validate = new Validate($rule,$message);
        $result = $validate->check($data);
        if (!$result){
            $msg['status'] = false;
            $msg['error_id'] = 10002355;
            $msg['message'] = $validate->getError();
            return json($msg);
        }
        $appVer = ['1.0.2','1.0.3','1.0.4'];
        // 不让更新
        if(in_array($data['app_ver'],$appVer) || $data['app_build_ver'] == '1.0.200008'){
            $msg['status'] = true;
            $msg['update'] = '0';
            $msg['message'] = '获取数据失败';
            return json($msg);
        }
        $updated_version = ['1.0.122','1.0.200007222']; // 存放要强制更新的版本
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $http = "{$http_type}{$_SERVER['SERVER_NAME']}";
        if ($data['app_ver'] != $data['app_build_ver'] && !empty($data['app_build_ver']) && (version_compare($data['app_ver'],$data['app_build_ver']) == -1)){
            $msg['status'] = true;
            switch ($data['app_type']) {
                case 'ios':
                    $msg['message'] = '获取成功';
                    $msg['update'] = (in_array($data['app_build_ver'],$updated_version))? '2': '1';
                    $msg['update_text'] = '请更新app，相信你可以获得更好的体验';
                    logs('ios',$data);
                    logs('ios',$msg);
                    return json($msg);
                    break;
                case 'android':
//                    $msg['download_link'] = "http://huaer.zanboon.com/apk/index/download_apk/app_type/android/app_build_ver/app-1.0.38";
                    $msg['download_link'] = "{$http}/apk/index/download_apk/app_type/android/app_build_ver/{$data['app_build_ver']}";
                    $msg['message'] = '获取成功';
                    $msg['update'] = (in_array($data['app_build_ver'],$updated_version))? '2': '1';
                    $msg['update_text'] = '请更新app，相信你可以获得更好的体验';
                    logs('android',$data);
                    logs('android',$msg);
                    return json($msg,200,[],['JSON_UNESCAPED_SLASHES']);
                    break;
                default:
                    $msg['status'] = false;
                    $msg['update'] = '1';
                    $msg['message'] = '不存在该app类型';
                    return json($msg);
            }
        }else{
            $msg['status'] = true;
            $msg['update'] = '0';
            $msg['message'] = '无需更新';
            return json($msg);
        }
    }
}
